﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
using System.ComponentModel;

namespace Chen.CefWebKit.Factory.UrlFilter
{
    public class UrlFileFilter : ResponseUrlFilterFacotory
    {
        UrlFileType type;
        public UrlFileFilter(string _url,UrlFileType _type, Action<IRequest, IResponse, byte[]> _action) : base(_url, _action)
        {
            type = _type;
        }
        public override bool IsFilterUrl(IRequest request, IResponse response)
        {
            string[] urls = request.Url.Split('?');
            if (urls.Count() > 0)
            {
                string filenameEx = urls[0].Split('.').LastOrDefault();
                if (!string.IsNullOrEmpty(filenameEx))
                {
                    filenameEx = filenameEx.ToLower();
                    switch (type)
                    {
                        case UrlFileType.JPG:
                            return filenameEx == "jpg";
                        case UrlFileType.BMP:
                            return filenameEx == "bmp";
                        case UrlFileType.PNG:
                            return filenameEx == "png";
                        case UrlFileType.TXT:
                            return filenameEx == "txt";
                        case UrlFileType.HTML:
                            return filenameEx == "html";
                        case UrlFileType.PDF:
                            return filenameEx == "pdf";
                        case UrlFileType.JS:
                            return filenameEx == "js";
                        case UrlFileType.CSS:
                            return filenameEx == "css";
                        case UrlFileType.MP4:
                            return filenameEx == "mp4";
                        case UrlFileType.FLV:
                            return filenameEx == "flv";
                        default:
                            return EnumToList<UrlFileType>().Any(i => i.EnumName == filenameEx.ToUpper());
                    }
                }
                //return Url.ToLower() == urls[0].ToLower();
            }
            return false;
        }
        /// <summary>
        /// 枚举转List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private List<EnumberEntity> EnumToList<T>()
        {
            List<EnumberEntity> list = new List<EnumberEntity>();

            foreach (var e in Enum.GetValues(typeof(T)))
            {
                EnumberEntity m = new EnumberEntity();
                object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute da = objArr[0] as DescriptionAttribute;
                    m.Desction = da.Description;
                }
                m.EnumValue = Convert.ToInt32(e);
                m.EnumName = e.ToString();
                list.Add(m);
            }
            return list;
        }
    }
    internal class EnumberEntity
    {
        /// <summary>  
        /// 枚举的描述  
        /// </summary>  
        public string Desction { set; get; }

        /// <summary>  
        /// 枚举名称  
        /// </summary>  
        public string EnumName { set; get; }

        /// <summary>  
        /// 枚举对象的值  
        /// </summary>  
        public int EnumValue { set; get; }
    }
    public enum UrlFileType
    {
        JPG,
        BMP,
        PNG,
        TXT,
        HTML,
        PDF,
        ANY,
        CSS,
        JS,
        MP4,
        FLV
    }
}
